<?php 

class tocode {

	private $go_list;
	public $go_code;
	public $go_content;
	public $cyrs;
	public $shop;
	public $count_time='';
	public $shishi_cai; //时时彩数据
	
	public function __construct() {
		$this->db = new CoreModel();
	}	

	public function config($shop=null,$type=null){
		$this->shop = $shop;		
	}
	
	public function get_run_tocode(){
		
	}
	
	public function returns(){
		
	}
	
	public function run_tocode(&$time='',$num=100,$cyrs='233'){
		if(empty($time))return false;
		if(empty($num))return false;
		if(empty($cyrs))return false;
		$this->times = $time;
		$this->num = $num;
		$this->cyrs = $cyrs;
		$this->get_code_user_html();
		$this->get_user_go_code();
	}
	
	//组团计算商品+时时彩
	public function team_run_tocode(&$time='',$num=100,$cyrs='233'){
		if(empty($time))return false;
		if(empty($num))return false;
		if(empty($cyrs))return false;
		$this->times = $time;
		$this->num = $num;
		$this->cyrs = $cyrs;
		$this->team_get_code_user_html();
		//$this->team_get_user_go_code();
	}
	private function team_get_code_user_html(){
		$time = $this->times;
		$num  = $this->num;
		$this->go_list = query("select * from `@#_member_team_record` where `time` < '$time' order by `id` DESC limit 0,$num",2);
		if($this->go_list  && count($this->go_list) >= $this->num){
			$this->team_get_code_dabai();
		}else{
			$this->team_get_code_yibai();
		}
	}
	private function team_get_user_go_code(){
// 		if(file_exists(G_SYSTEM.'modules/goodspecify/lib/'.'itocode.class.php')){
// 			$itocode = System::load_app_class("itocode","goodspecify");
// 			$itocode->team_go_itocode($this->shop,$this->go_code,$this->go_content,$this->count_time);
// 		}
		$this->team_get_code_user_html();
	}
	//抢购小于 100人的计算方式
	private function team_get_code_yibai(){
		$time = $this->times;
		$cyrs = $this->cyrs;
		$h=abs(date("H",$time));
		$i=date("i",$time);
		$s=date("s",$time);
		$w=substr($time,11,3);
		$num= $h.$i.$s.$w;
		if(!$cyrs){
			$cyrs=1;
		}
		//$this->go_code = 10000001+fmod($num*100,$cyrs);
		
		$total_h = $num*100 + (int)$this->shishi_cai['opencode'];
		//file_put_contents('time.txt', $total_h.'+'.$num.'+'.$cyrs);
		$this->go_code = 10000001+fmod($total_h,$cyrs);
		
		$this->go_content = false;
	}
	//002953485
	private function team_get_code_dabai(){
		$go_list = $this->go_list;
		$html=array();
	
		//统计100条下单记录的时间
		$count_time = 0;
		foreach($go_list as $key=>$v){
			$html[$key]['time'] = $v['time'];
			$html[$key]['username'] = $v['username'];
			$html[$key]['uid'] = $v['uid'];
			$html[$key]['shopid'] = $v['shopid'];
			$html[$key]['shopname'] = $v['shopname'];
			$html[$key]['shopqishu'] = $v['shopqishu'];
			$html[$key]['gonumber'] = $v['gonumber'];
			$h=abs(date("H",$v['time']));
			$i=date("i",$v['time']);
			$s=date("s",$v['time']);
			list($time,$ms) = explode(".",$v['time']);
			$time = $h.$i.$s.$ms;
			$html[$key]['time_add'] = $time;
			$count_time += $time;
		}
	
		$this->go_content = serialize($html);
		$this->count_time=$count_time;
		
		$total_h = $this->count_time + (int)$this->shishi_cai['opencode'];
		
		$this->go_code = 10000001+fmod($total_h,$this->cyrs);
	}
	
	private function get_user_go_code(){
		//if(file_exists(G_SYSTEM.'modules/goodspecify/lib/'.'itocode.class.php')){
			//$itocode = new itocode();
			//$itocode->go_itocode($this->shop,$this->go_code,$this->go_content,$this->count_time);
		//}
		$this->get_code_user_html();
	}
	
	private function get_code_user_html(){
		$time = $this->times;
		$num  = $this->num;
		$this->go_list = query("select * from `@#_member_go_record` where `time` < '$time' order by `id` DESC limit 0,$num",2);
		//如果订单大于等于100
		if($this->go_list  && count($this->go_list) >= $this->num){
			$this->get_code_dabai();
		}else{
			$this->get_code_yibai();
		}
	}
		
	//抢购小于 100人的计算方式
	private function get_code_yibai(){
		$time = $this->times;
		$cyrs = $this->cyrs;
		$h=abs(date("H",$time));
		$i=date("i",$time);
		$s=date("s",$time);
		$w=substr($time,11,3);
		$num= $h.$i.$s.$w;
		if(!$cyrs){
			$cyrs=1;
		}
		
		//当前商品最后购买的时间戳171047700*100+时时彩开奖码
		$total_h = $num*100 + (int)$this->shishi_cai['opencode'];
		//file_put_contents('time.txt', $total_h.'+'.$num.'+'.$cyrs);
		$this->go_code = 10000001+fmod($total_h,$cyrs);
		$this->go_content = false;
	}
	
	//002953485
	private function get_code_dabai(){
		$go_list = $this->go_list;
		$html=array();
		
		//统计100条下单记录的时间
		$count_time = 0;
		foreach($go_list as $key=>$v){
			$html[$key]['time'] = $v['time'];	
			$html[$key]['username'] = $v['username'];	
			$html[$key]['uid'] = $v['uid'];
			$html[$key]['shopid'] = $v['shopid'];	
			$html[$key]['shopname'] = $v['shopname'];	
			$html[$key]['shopqishu'] = $v['shopqishu'];
			$html[$key]['gonumber'] = $v['gonumber'];			
			$h=abs(date("H",$v['time']));
			$i=date("i",$v['time']);
			$s=date("s",$v['time']);	
			list($time,$ms) = explode(".",$v['time']);
			$time = $h.$i.$s.$ms;
			$html[$key]['time_add'] = $time;
			$count_time += $time;
		}
		
		$this->go_content = serialize($html);
		
		$this->count_time = $count_time;
		
		$total_h = $this->count_time + (int)$this->shishi_cai['opencode'];
		
		$this->go_code = 10000001+fmod($total_h,$this->cyrs);
	}
	
	
	
	
			
}